#include <iostream>
#include <exception>
#include <gflags/gflags.h>
#include "MagickImage.h"
#include "OpenCVImage.h"
#include "ReadableImage.h"
#include "CompositeImage.h"
#include "PanoramaDownloader.h"

using namespace std;

const int ERROR_INVALID_FLAGS = 1;

DEFINE_string(server,
              "http://cbk0.google.com/cbk",
              "Server to hit with request");
DEFINE_string(pano, "gDFBdoet3xk-BmPPlDrRRg", "Google panorama ID");
DEFINE_int32(zoom, 3, "Tile zoom");
DEFINE_int32(world_width, 3328, "Total panorama width");
DEFINE_int32(world_height, 1664, "Total panorama height");
DEFINE_int32(tile_width, 512, "Tile width");
DEFINE_int32(tile_height, 512, "Tile height");
DEFINE_int32(timeout, 1000, "HTTP timeout");
DEFINE_string(user_agent,
              "Mozilla/5.0 (X11; U; Linux i686; en-US) "
              "AppleWebKit/533.4 (KHTML, like Gecko) "
              "Chrome/5.0.375.99 Safari/533.4",
              "User agent specified in HTTP request");

int main(int argc, char **argv) {
  const string USAGE(
      "Downloads a 360 degree panorama from Google Street View. "
      "Sample usage:\n" + string(argv[0]) + " [flags] <outfile>");
  google::SetUsageMessage(USAGE);
  google::ParseCommandLineFlags(&argc, &argv, true);

  PanoramaDownloader downloader(FLAGS_pano, FLAGS_server, FLAGS_timeout,
      FLAGS_user_agent, FLAGS_world_width, FLAGS_world_height, FLAGS_tile_width,
      FLAGS_tile_height, FLAGS_zoom);

  CompositeImage<OpenCVImage> final;
  downloader.download(&final);
  cout << "Downloaded image!" << endl;

  OpenCVImage flat;
  final.flatten(&flat);

  flat.save("output.jpg");

  return 0;
}

